//
//  DSPickView.m
//  zhongcaixun
//
//  Created by apple on 2018/8/9.
//  Copyright © 2018年 sunpeng. All rights reserved.
//

#import "DSPickView.h"
#import "BasePickerView.h"
@interface DSPickView()<UIPickerViewDelegate,UIPickerViewDataSource>

@property (strong, nonatomic) UIButton *cancelJKAPPBtn;
@property (strong, nonatomic) UIButton *sureJKAPPBtn;
@property (strong, nonatomic) UILabel *JKAPPTitleLabel;
@property (strong, nonatomic) UIPickerView *pikerView;
@property (nonatomic,strong) NSArray *JKAPPDataArray;//picker数据
@property (nonatomic,strong) NSMutableArray *selectArray;
@property (nonatomic,strong) NSMutableArray *selectIndexArray;

@property (nonatomic,strong) NSArray *secondArray;//picker第二行动态数据

//时间选择器
@property (nonatomic, strong) UIDatePicker *startDateView;
@property (nonatomic, strong) UIDatePicker *endDateView;

@end



@implementation DSPickView

- (instancetype)initWithTitle:(NSString *)title data:(NSArray *)JKAPPDataArray {
    CGRect rect = CGRectMake(0, 0, KScreen_W, KScreen_H);
    self = [super initWithFrame:rect];
    if (self) {
        
        UIControl * bgControl = [[UIControl alloc] initWithFrame:rect];
        [bgControl addTarget:self action:@selector(clearSubEvent) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:bgControl];
        
//添加工具栏 + picker
        UIView * toolbarView = [[UIView alloc] initWithFrame:CGRectMake(0, KScreen_H - 238, KScreen_W, 238)];
        toolbarView.backgroundColor = [UIColor whiteColor];
        [toolbarView addSubview:self.sureJKAPPBtn];
        [toolbarView addSubview:self.cancelJKAPPBtn];
        
        if (title) {//添加标题
            [toolbarView addSubview:self.JKAPPTitleLabel];
            self.JKAPPTitleLabel.text = title;
        }
        
        UIView *JKAPPLineViewView = [[UIView alloc] initWithFrame:CGRectMake(0, 49, KScreen_W, 1)];
        JKAPPLineViewView.backgroundColor = [UIColor colorWithHexCode:@"#000000" alpha:0.06];
        [_pikerView addSubview:JKAPPLineViewView];
        
//picker view
        _JKAPPDataArray = [NSArray arrayWithArray:JKAPPDataArray];
        for (int i = 0; i < _JKAPPDataArray.count; i ++) {
            NSArray *defaultSelect = _JKAPPDataArray[i];
            if(defaultSelect.count != 0){
                id obj = [defaultSelect firstObject];
                if ([obj isKindOfClass:[NSArray class]]) {
                    if (i == 1) {
                        _secondArray = [NSArray arrayWithArray:defaultSelect];
                        _JKAPPDataArray = @[[_JKAPPDataArray firstObject],[_secondArray firstObject]];
                    }
                    [self.selectArray replaceObjectAtIndex:i withObject:[[defaultSelect firstObject] firstObject]];
                }else {//string
                    [self.selectArray replaceObjectAtIndex:i withObject:[defaultSelect firstObject]];
                }
            }
        }
        
        _pikerView = [[BasePickerView alloc] initWithFrame:CGRectMake(0, 50, rect.size.width, 188)];
        _pikerView.backgroundColor = [UIColor whiteColor];
        _pikerView.delegate = self;
        _pikerView.dataSource = self;
        [toolbarView addSubview:_pikerView];
        [self addSubview:toolbarView];
        
        
//        CGFloat y = 188/2 - 19;
//        JKAPPLineViewView = [[UIView alloc] initWithFrame:CGRectMake(12, y, KScreen_W-24, 1)];
//        JKAPPLineViewView.backgroundColor = [UIColor colorWithHexCode:@"#000000" alpha:0.12];
//        [_pikerView addSubview:JKAPPLineViewView];
//        
//        y += 38;
//        JKAPPLineViewView = [[UIView alloc] initWithFrame:CGRectMake(12, y, KScreen_W-24, 1)];
//        JKAPPLineViewView.backgroundColor = [UIColor colorWithHexCode:@"#000000" alpha:0.06];
//        [_pikerView addSubview:JKAPPLineViewView];
    }
    return self;
}
//一个picker
- (instancetype)initWithTitle:(NSString *)title data:(NSArray *)JKAPPDataArray seleteIndex:(NSInteger)seleteIndex{
    CGRect rect = CGRectMake(0, 0, KScreen_W, KScreen_H);
    self = [super initWithFrame:rect];
    if (self) {
        
        UIControl * bgControl = [[UIControl alloc] initWithFrame:rect];
        [bgControl addTarget:self action:@selector(clearSubEvent) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:bgControl];
        
//添加工具栏 + picker
        UIView * toolbarView = [[UIView alloc] initWithFrame:CGRectMake(0, KScreen_H - 238, KScreen_W, 238)];
        toolbarView.backgroundColor = [UIColor whiteColor];
        [toolbarView addSubview:self.sureJKAPPBtn];
        [toolbarView addSubview:self.cancelJKAPPBtn];
        
        if (title) {//添加标题
            [toolbarView addSubview:self.JKAPPTitleLabel];
            self.JKAPPTitleLabel.text = title;
        }
        
        UIView *JKAPPLineViewView = [[UIView alloc] initWithFrame:CGRectMake(0, 49, KScreen_W, 1)];
        JKAPPLineViewView.backgroundColor = [UIColor colorWithHexCode:@"#000000" alpha:0.06];
        [_pikerView addSubview:JKAPPLineViewView];
        
//picker view
        _JKAPPDataArray = [NSArray arrayWithArray:JKAPPDataArray];
        for (int i = 0; i < _JKAPPDataArray.count; i ++) {
            NSArray *defaultSelect = _JKAPPDataArray[i];
            if(defaultSelect.count != 0){
                id obj = [defaultSelect firstObject];
                if ([obj isKindOfClass:[NSArray class]]) {
                    if (i == 1) {
                        _secondArray = [NSArray arrayWithArray:defaultSelect];
                        _JKAPPDataArray = @[[_JKAPPDataArray firstObject],[_secondArray firstObject]];
                    }
                    [self.selectArray replaceObjectAtIndex:i withObject:[[defaultSelect firstObject] firstObject]];
                }else {//string
                    [self.selectArray replaceObjectAtIndex:i withObject:[defaultSelect firstObject]];
                }
            }
        }
        
        _pikerView = [[BasePickerView alloc] initWithFrame:CGRectMake(0, 50, rect.size.width, 188)];
        _pikerView.backgroundColor = [UIColor whiteColor];
        _pikerView.delegate = self;
        _pikerView.dataSource = self;
        [toolbarView addSubview:_pikerView];
        [self addSubview:toolbarView];
        
        [_pikerView selectRow:seleteIndex inComponent:0 animated:NO];
//        CGFloat y = 188/2 - 19;
//        JKAPPLineViewView = [[UIView alloc] initWithFrame:CGRectMake(12, y, KScreen_W-24, 1)];
//        JKAPPLineViewView.backgroundColor = [UIColor colorWithHexCode:@"#000000" alpha:0.12];
//        [_pikerView addSubview:JKAPPLineViewView];
//
//        y += 38;
//        JKAPPLineViewView = [[UIView alloc] initWithFrame:CGRectMake(12, y, KScreen_W-24, 1)];
//        JKAPPLineViewView.backgroundColor = [UIColor colorWithHexCode:@"#000000" alpha:0.06];
//        [_pikerView addSubview:JKAPPLineViewView];
    }
    return self;
}

#pragma mark - 时间
- (instancetype)initWithTitle:(NSString *)title beginTime:(NSString *)bTime JKAPPEndTime:(NSString *)eTime {
    CGRect rect = CGRectMake(0, 0, KScreen_W, KScreen_H);
    self = [super initWithFrame:rect];
    if (self) {
        
        UIControl * bgControl = [[UIControl alloc] initWithFrame:rect];
        [bgControl addTarget:self action:@selector(clearSubEvent) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:bgControl];
        
        //添加工具栏 + picker
        UIView * toolbarView = [[UIView alloc] initWithFrame:CGRectMake(0, KScreen_H - 238, KScreen_W, 238)];
        toolbarView.backgroundColor = ColorHex(@"#F7F7F7");
        [toolbarView addSubview:self.sureJKAPPBtn];
        [toolbarView addSubview:self.cancelJKAPPBtn];
        
        if (title) {//添加标题
            [toolbarView addSubview:self.JKAPPTitleLabel];
            self.JKAPPTitleLabel.text = title;
        }
        
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, KScreen_W/2.0, 40)];
        label.backgroundColor = [UIColor whiteColor];
        label.font = [UIFont systemFontOfSize:14];
        label.textColor = ColorHex(@"333333");
        label.textAlignment = NSTextAlignmentCenter;
        label.text = bTime;
        [toolbarView addSubview:label];
        label = [[UILabel alloc] initWithFrame:CGRectMake(KScreen_W/2.0, 50, KScreen_W/2.0, 40)];
        label.backgroundColor = [UIColor whiteColor];
        label.font = [UIFont systemFontOfSize:14];
        label.textColor = ColorHex(@"333333");
        label.textAlignment = NSTextAlignmentCenter;
        label.text = @"结束日期";
        [toolbarView addSubview:label];
        
        self.startDateView = [[UIDatePicker alloc] init];
        self.startDateView.backgroundColor = [UIColor whiteColor];
        self.startDateView.frame = CGRectMake(- KScreen_W/4.0, 90 - 148*0.05, KScreen_W, 148*1.1);
        self.startDateView.datePickerMode = UIDatePickerModeDate;
//        [self.startDateView setLocale:[NSLocale systemLocale]];
        [self.startDateView setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]];
        [toolbarView addSubview:self.startDateView];
        [self.startDateView addTarget:self action:@selector(pickerValueChange:) forControlEvents:UIControlEventValueChanged];
        self.startDateView.transform = CGAffineTransformMakeScale(.5, .9);
        
        self.endDateView = [[UIDatePicker alloc] init];
        self.endDateView.backgroundColor = [UIColor whiteColor];
        self.endDateView.frame = CGRectMake(KScreen_W/2.0 - KScreen_W/4.0, 90 - 148*0.05, KScreen_W, 148*1.1);
        self.endDateView.datePickerMode = UIDatePickerModeDate;
//        [self.endDateView setLocale:[NSLocale systemLocale]];
        [self.endDateView setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]];
        [toolbarView addSubview:self.endDateView];
        [self.endDateView addTarget:self action:@selector(pickerValueChange:) forControlEvents:UIControlEventValueChanged];
        self.endDateView.maximumDate = [NSDate date];
        
        self.endDateView.transform = CGAffineTransformMakeScale(.5, .9);
        
        
        
        [self addSubview:toolbarView];
        
//        CGFloat y = 148/2 - 19;
//        UIView *JKAPPLineViewView = [[UIView alloc] initWithFrame:CGRectMake(0, y, KScreen_W, 1)];
//        JKAPPLineViewView.backgroundColor = [UIColor colorWithHexCode:@"#000000" alpha:0.06];
//        [_pikerView addSubview:JKAPPLineViewView];
//
//        y += 38;
//        JKAPPLineViewView = [[UIView alloc] initWithFrame:CGRectMake(0, y, KScreen_W, 1)];
//        JKAPPLineViewView.backgroundColor = [UIColor colorWithHexCode:@"#000000" alpha:0.06];
//        [_pikerView addSubview:JKAPPLineViewView];
    }
    return self;
}

- (void)pickerValueChange:(UIDatePicker *)JKAPPSender  {
    if (JKAPPSender == self.startDateView) {
        self.endDateView.minimumDate = JKAPPSender.date;
    }else if(JKAPPSender == self.endDateView) {
        self.startDateView.maximumDate = JKAPPSender.date;
    }
}

#pragma mark - 懒加载
- (UIButton *)sureJKAPPBtn {
    if (!_sureJKAPPBtn) {
        _sureJKAPPBtn = [[UIButton alloc] init];
        _sureJKAPPBtn.frame = CGRectMake(KScreen_W - 25 - 44, 0, 44, 50);
        [_sureJKAPPBtn setTitle:@"确定" forState:UIControlStateNormal];
        [_sureJKAPPBtn setTitleColor:MainColor forState:UIControlStateNormal];
        _sureJKAPPBtn.titleLabel.font = Font(15);
        [_sureJKAPPBtn addTarget:self action:@selector(sureJKAPPBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sureJKAPPBtn;
}

- (UIButton *)cancelJKAPPBtn {
    if (!_cancelJKAPPBtn) {
        _cancelJKAPPBtn = [[UIButton alloc] init];
        _cancelJKAPPBtn.frame = CGRectMake(25, 4, 44, 40);
        [_cancelJKAPPBtn setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelJKAPPBtn setTitleColor:NineColor forState:UIControlStateNormal];
        _cancelJKAPPBtn.titleLabel.font = Font(15);
        [_cancelJKAPPBtn addTarget:self action:@selector(cancelJKAPPBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelJKAPPBtn;
}

-(UILabel *)JKAPPTitleLabel {
    if (!_JKAPPTitleLabel) {
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(70, 4, KScreen_W- 140, 40)];
        label.font = Font(15);
        label.textColor = ColorHex(@"333333");
        label.textAlignment = NSTextAlignmentCenter;
        _JKAPPTitleLabel = label;
    }
    return _JKAPPTitleLabel;
}

-(NSMutableArray *)selectArray {
    if (!_selectArray) {
        _selectArray = [NSMutableArray arrayWithCapacity:_JKAPPDataArray.count];
        for (int i = 0; i < _JKAPPDataArray.count; i ++) {
            [_selectArray addObject:@""];
        }
    }
    return _selectArray;
}

-(NSMutableArray *)selectIndexArray {
    if (!_selectIndexArray) {
        _selectIndexArray = [NSMutableArray arrayWithCapacity:_JKAPPDataArray.count];
        for (int i = 0; i < _JKAPPDataArray.count; i ++) {
            [_selectIndexArray addObject:@0];
        }
    }
    return _selectIndexArray;
}

#pragma mark - picker
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return _JKAPPDataArray.count;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    
    NSArray *array = _JKAPPDataArray[component];
    if (![array isKindOfClass:[NSArray class]]) {
        return 0;//数据错误
        NSLog(@"DSPickerView 数据源格式错误");
    }
    if(component == 1 && [_selectIndexArray[0] intValue] == [_JKAPPDataArray[0] count]){
        return self.maxRow;
    }
    return array.count;
    
//    id obj = [array firstObject];
//    if ([obj isKindOfClass:[NSArray class]]) {//i第二列不是固定数据
//
//
//        [self.selectArray replaceObjectAtIndex:i withObject:[[defaultSelect firstObject] firstObject]];
//    }else {//string
//        return array.count;
//    }
}

- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    
    NSArray *array = _JKAPPDataArray[component];
    return array[row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    
    if (component == 0 && _JKAPPDataArray.count == 2) {
        [pickerView reloadComponent:1];
    }
    
    NSString *str = _JKAPPDataArray[component][row];
    if (str) {
        [self.selectArray replaceObjectAtIndex:component withObject:str];
    }
    
    [self.selectIndexArray replaceObjectAtIndex:component withObject:@(row)];
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 38.0;
}


- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    CGFloat wid = 120;
    if (_JKAPPDataArray.count == 1) {
        wid = KScreen_W;
    }
    UILabel *myView = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, wid, 30)];
    myView.backgroundColor = [UIColor clearColor];
    myView.textAlignment = NSTextAlignmentCenter;
    myView.font = [UIFont systemFontOfSize:18];
    myView.textColor = ColorHex(@"161616");
    myView.text = _JKAPPDataArray[component][row];
    return myView;
}

- (void)cancelJKAPPBtnClick:(UIButton *)JKAPPSender {
    [self clearSubEvent];
}


- (void)sureJKAPPBtnClick:(UIButton *)JKAPPSender {
    if (self.sureBlock) {
        if (self.startDateView) {//返回时间
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
            [dateFormatter setDateFormat:@"yyyy-MM-dd"];
            NSArray *array = @[[dateFormatter stringFromDate:self.startDateView.date],[dateFormatter stringFromDate:self.endDateView.date]];
            self.sureBlock(array, nil);
        }else {
            self.sureBlock(self.selectArray, self.selectIndexArray);
        }
    }
    [self clearSubEvent];
}


-(void)show{
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    self.frame = window.bounds;
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    [window addSubview:self];
}

- (void)clearSubEvent {
    if (self.cancelBlock) {
        self.cancelBlock();
    }
    [self removeFromSuperview];
}


@end
